package cn.itcast.gateway.filters;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * TODO
 *
 * @Author mr.wei
 * @Date 2025/1/3 下午4:00
 */
@Order(-1)
@Component
public class AuthorizeFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter (ServerWebExchange exchange, GatewayFilterChain chain) {
        MultiValueMap<String,String> queryParams = exchange.getRequest ().getQueryParams ();
       String a= queryParams.getFirst ("authorization");
        if ("admin".equals (a)) {
            return chain.filter (exchange);
        }
        exchange.getResponse ().setStatusCode (HttpStatus.FORBIDDEN);
        return exchange.getResponse ().setComplete ();
    }
}
